package com.brayan.melinxa.gui.wizard;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextPane;

public class Wizard extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	List<JPanel> listOfPanels = null;
	int idx = 0;
	String title = null;
	JButton btnBack = new JButton(ResourceBundle
			.getBundle("locales.wizard").getString("Wizard.btnBack.text")); //$NON-NLS-1$ //$NON-NLS-2$
	JButton btnNext = new JButton(ResourceBundle
			.getBundle("locales.wizard").getString("Wizard.btnNext.text")); //$NON-NLS-1$ //$NON-NLS-2$
	JFrame parentFrame = null;
	private JPanel panelContent;
	private JPanel panel_Navigation;

	public Wizard(String title, List<JPanel> listOfPanels, JFrame parentFrame) {
		this.title = title;
		this.listOfPanels = listOfPanels;
		this.parentFrame = parentFrame;
		init();
	}

	/**
	 * @wbp.parser.constructor
	 */
	public Wizard(String title, JFrame parentFrame) {
		this.title = title;
		this.parentFrame = parentFrame;
		init();
	}

	private void init() {
		setBackground(Color.WHITE);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 450, 0 };
		gridBagLayout.rowHeights = new int[] { 32, 233, 35, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 1.0, 0.0, Double.MIN_VALUE };
		setLayout(gridBagLayout);

		JPanel panel_WizardBar = new JPanel();
		panel_WizardBar.setBackground(Color.WHITE);
		GridBagConstraints gbc_panel_WizardBar = new GridBagConstraints();
		gbc_panel_WizardBar.fill = GridBagConstraints.HORIZONTAL;
		gbc_panel_WizardBar.anchor = GridBagConstraints.NORTH;
		gbc_panel_WizardBar.insets = new Insets(0, 0, 5, 0);
		gbc_panel_WizardBar.gridx = 0;
		gbc_panel_WizardBar.gridy = 0;
		add(panel_WizardBar, gbc_panel_WizardBar);
		GridBagLayout gbl_panel_WizardBar = new GridBagLayout();
		gbl_panel_WizardBar.columnWidths = new int[] { 450, 0 };
		gbl_panel_WizardBar.rowHeights = new int[] { 0, 0, 0, 0, 0, 1, 0, 0 };
		gbl_panel_WizardBar.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gbl_panel_WizardBar.rowWeights = new double[] { 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0,
				Double.MIN_VALUE };
		panel_WizardBar.setLayout(gbl_panel_WizardBar);

		JTextPane textPane = new JTextPane();
		textPane.setContentType(ResourceBundle.getBundle("locales.wizard").getString("Wizard.textPane.contentType")); //$NON-NLS-1$ //$NON-NLS-2$
		textPane.setEditable(false);
		textPane.setText("<strong>"+title+"</strong>"); //$NON-NLS-1$ //$NON-NLS-2$
		GridBagConstraints gbc_textPane = new GridBagConstraints();
		gbc_textPane.insets = new Insets(0, 0, 5, 0);
		gbc_textPane.fill = GridBagConstraints.BOTH;
		gbc_textPane.gridx = 0;
		gbc_textPane.gridy = 1;
		panel_WizardBar.add(textPane, gbc_textPane);
		
		JSeparator separator = new JSeparator();
		GridBagConstraints gbc_separator = new GridBagConstraints();
		gbc_separator.fill = GridBagConstraints.BOTH;
		gbc_separator.insets = new Insets(0, 0, 5, 0);
		gbc_separator.gridx = 0;
		gbc_separator.gridy = 2;
		panel_WizardBar.add(separator, gbc_separator);

		panelContent = new JPanel();
		panelContent.setBackground(Color.WHITE);
		GridBagConstraints gbc_panelContent = new GridBagConstraints();
		gbc_panelContent.insets = new Insets(0, 0, 5, 0);
		gbc_panelContent.fill = GridBagConstraints.BOTH;
		gbc_panelContent.gridx = 0;
		gbc_panelContent.gridy = 1;
		add(panelContent, gbc_panelContent);
		panelContent.setLayout(new BoxLayout(panelContent, BoxLayout.X_AXIS));

		panel_Navigation = new JPanel();
		panel_Navigation.setBackground(Color.WHITE);
		GridBagConstraints gbc_panel_Navigation = new GridBagConstraints();
		gbc_panel_Navigation.fill = GridBagConstraints.BOTH;
		gbc_panel_Navigation.gridx = 0;
		gbc_panel_Navigation.gridy = 2;
		add(panel_Navigation, gbc_panel_Navigation);
		GridBagLayout gbl_panel_Navigation = new GridBagLayout();
		gbl_panel_Navigation.columnWidths = new int[] { 450, 0 };
		gbl_panel_Navigation.rowHeights = new int[] { 1, 33, 0 };
		gbl_panel_Navigation.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gbl_panel_Navigation.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
		panel_Navigation.setLayout(gbl_panel_Navigation);

		JSeparator separator_1 = new JSeparator();
		GridBagConstraints gbc_separator_1 = new GridBagConstraints();
		gbc_separator_1.insets = new Insets(0, 0, 5, 0);
		gbc_separator_1.gridx = 0;
		gbc_separator_1.gridy = 0;
		panel_Navigation.add(separator_1, gbc_separator_1);

		JPanel panel_1 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		panel_1.setBackground(Color.WHITE);
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.anchor = GridBagConstraints.EAST;
		gbc_panel_1.fill = GridBagConstraints.VERTICAL;
		gbc_panel_1.gridx = 0;
		gbc_panel_1.gridy = 1;
		panel_Navigation.add(panel_1, gbc_panel_1);
		panel_1.add(btnBack);
		panel_1.add(btnNext);

		btnNext.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				next();
			}
		});

		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				back();
			}
		});

		if (listOfPanels.size() == 1) {
//			btnBack.setVisible(false);
//			btnNext.setVisible(false);
			panel_Navigation.setVisible(false);
		}
		
		
		if (this.listOfPanels != null && !this.listOfPanels.isEmpty())
			setup();
	}

	private void setup() {
		panelContent.removeAll();
		// panelContent.revalidate();
		panelContent.add(listOfPanels.get(idx));
		validateBtnAccess();
	}

	private void next() {
		if (idx < listOfPanels.size() - 1) {
			idx++;
			setup();

		}
	}

	private void back() {
		if (idx > 0) {
			idx--;
			setup();
		}
	}

	/**
	 * enable the rigth buttons
	 */
	private void validateBtnAccess() {
		btnBack.setEnabled(idx > 0);
		btnNext.setEnabled(idx < listOfPanels.size() - 1);
	}

}
